package com.woniuxy.login.service.impl;

import com.woniuxy.common.CommonResult;
import com.woniuxy.login.client.PersonServiceFeignClient;
import com.woniuxy.login.client.dto.PlUser;
import com.woniuxy.login.config.JwtConfig;
import com.woniuxy.login.exception.SeckillException;
import com.woniuxy.login.service.UserService;
import com.woniuxy.login.vo.UserVo;
import com.woniuxy.security.util.JwtUtil;
import com.woniuxy.security.util.RsaUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Date: 2021/6/30:12:07  <br/>
 * Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private PersonServiceFeignClient client;

    @Resource
    private JwtConfig jwtConfig;

    @Override
    public String login(UserVo userVo) {
        CommonResult<PlUser> user = client.selectUser(userVo.getUsername());
        PlUser plUser = user.getData();
        if (plUser == null){
            throw new SeckillException(500,"帐号不存在，请前往注册");
        }
        if (userVo.getPassword().equals(plUser.getPassword())){
            Map claims = new HashMap();
            claims.put("id",plUser.getId());
            claims.put("username",plUser.getUsername());
            claims.put("name",plUser.getName());
            String token = JwtUtil.createToken(claims, RsaUtil.getPrivateKey(jwtConfig.getPriPath()));
            return token;
        }
        return null;
    }
}
